package com.tanjun.publicservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.common.enums.err.DefaultErrCode;
import com.tanjun.common.exception.BizException;
import com.tanjun.publicservice.web.orm.entity.SysDict;
import com.tanjun.publicservice.web.orm.mapper.SysDictMapper;
import com.tanjun.publicservice.web.service.SysDictDataService;
import com.tanjun.publicservice.web.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author tanjun
* @description 针对表【sys_dict(字典表)】的数据库操作Service实现
* @createDate 2025-01-09 21:42:04
*/
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict>
    implements SysDictService{
    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 新增或者修改字典基本信息
     *
     * @param sysDict
     */
    @Override
    public void saveOrUpdateDic(SysDict sysDict) {
        long countCode=this.count(QueryChain.of(SysDict.class)
                .ne(SysDict::getId,sysDict.getId(),sysDict.getId()!=null)
                .eq(SysDict::getDictCode,sysDict.getDictCode())
        );
        if(countCode>0){
            throw new BizException("字典编码已存在");
        }
        if(sysDict.getId()!=null){
            this.updateById(sysDict);
        }
        else {
            this.save(sysDict);
        }
    }

    /**
     * 删除字典信息
     *
     * @param id id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delDict(Long id) {
        SysDict dict=this.getById(id);
        if(dict==null){
            throw new BizException(DefaultErrCode.NO_DATA);
        }
        this.removeById(id);
        sysDictDataService.delDictData(dict.getDictCode());
    }
}




